Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
Listing 19.12 The List Form’s Load event procedure.
    Private Sub Form_Load()

    Dim i As Integer, buf As String

    ‘ When the form loads, go through the entire database
    ‘ file reading each record and adding it to the list box.

    For i = 1 To NumberOfRecords
        Get #FileNum, i, CR
        buf = RTrim$(CR.LName)
        buf = buf & “, ” & RTrim$(CR.FName)
        buf = buf & “    ” & RTrim$(CR.Address)
        buf = buf & “, ” & RTrim$(CR.City)
        buf = buf & “ ” & RTrim$(CR.State)
        buf = buf & “ ” & RTrim$(CR.Zip)
        List1.AddItem buf
    Next i

    End Sub
The final item is the Resize event procedure shown in Listing 19.13. Code in this procedure sets the size of the List Box to fill the form, a familiar technique. Listing 19.13 The List Form’s Resize event procedure.
    Private Sub Form_Resize()

    ‘ Make the list box the same size as the form.

    List1.Left = 0
    List1.TOP = 0
    List1.Width = frmList.ScaleWidth
    List1.Height = frmList.ScaleHeight

    End Sub
#### Additional Features For The Address Database Our address database works pretty well. We can enter new addresses, view and edit existing ones, and so on. After you use it for a short time, however, the shortcomings will become apparent. The most important feature it lacks, at least in my mind, is the ability to search for records based on a person’s name. You certainly don’t want to scroll through several hundred addresses just to find one. The ability to print envelopes would be nice, too, as would fields for additional information, such as phone numbers. A more serious drawback is the way the program handles deleted records. When you delete a record, the program does not actually remove that record from the file. Rather, as was shown in Listing 19.4, it simply erases the contents of the record’s fields. The blank record still takes up space in the file and will still display as we scroll through the database. There are several approaches we can take to solve this problem: • Add code that will skip over blank records during the scrolling process or when the records are listed. The blank records will still take up disk space, but at least they won’t be displayed. • Change the code. Instead of always adding new records at the end of the file, it will look through the file for an empty record and place the new one there, moving to the end only if there are no empty spaces. • Compact the database file on a regular basis—for example, each time the program starts. Compacting consists of going through the file looking for empty records and filling them, usually by moving data from the last record in the file to the blank record. I leave these improvements to you, if you are interested. Remember, the purpose of this program is to show you how to create a database program without using Visual Basic’s specialized database tools. This may be the “old” way of doing it, but remember that I am not using “old” in a pejorative sense. Just because it is the old way does not automatically make it inferior to the new way. Now that you know how it is handled, you can make up your own mind. ### Using Visual Basic’s Database Tools If you do not want to program your database application from scratch, you can use Visual Basic’s tools to simplify your task. In fact, Visual Basic’s database toolbox contains an embarrassment of riches; sometimes, the problem faced by a new programmer is trying to figure out all of the ways that Visual Basic can do database programming. That, however, is a topic for Chapters 21 and 22. For the present, I want to show you the fundamentals of using the specialized database tools in a program. Once you have the basics under your belt, you will be ready to take on some more advanced database topics in later chapters. The Jet database engine is your highly skilled database programming assistant. In this context, the term engine is not used to mean a source of power—as in an automobile engine—but rather a mechanism or device. The Jet database engine sits between your Visual Basic program and the database file that contains the records. The program pulls levers and pushes buttons on its side of the engine, and the gears, cams, and springs in the engine go to work and perform the requested action on the database. The result, if requested, pops out of a slot ready for program use. Of course, “pulling levers and pushing buttons” means sending commands, but the basic idea is the same. We tell the engine what we want to accomplish, and the engine worries about how to do it. Part of the beauty of this system is the ability of the database engine to work with several different kinds of databases, including those shown here: • Microsoft Access • Microsoft Excel Worksheets • Lotus 1-2-3 spreadsheets • Paradox versions 3.x, 4.x, and 5.x • dBASE III, IV, and 5 • Microsoft FoxPro, versions 2, 2.5, 2.6, 3, and DBC • ODBC In other words, we can write a Visual Basic program to manipulate data in any of these database formats. Of course, all types of databases use the same logical file-table-record-field format, but each type of database file uses its own proprietary method of storing the data and its related information. The Jet database engine translates your program’s requests into the specific commands required by the database file format in use. The Jet database engine that Visual Basic uses is actually the same one used by Microsoft’s Access database program. A Visual Basic program can interact with a database on two levels: • The most common is to connect the Visual Basic program to an existing database. The program can add, delete, print, and otherwise manipulate the data in the database, but it cannot change the structure of the database. In other words, it cannot create a new database, add or delete tables in an existing database, or change the field structure of records in a table. • The second level of access permits not only database manipulation, but database creation and modification as well. Thus, the user of the Visual Basic program can create a database from scratch, specifying the tables and fields it is to contain. The second level is considerably more complicated to program, and we will not pursue it further in this book. The first approach—where the database structure already exists and is fixed—is by far the most useful and the one you will encounter most often. The database that our Visual Basic program will manipulate must already exist. It may be a client’s database that contains thousands of records, or it might be a brand-new database created as part of the program development process. A new database can be empty—that is, contain no data—but its table and record structure must be defined. How do you, the Visual Basic developer, create a database to be used by your program? You use a tool provided with Visual Basic, called the Visual Data Manager, or VisData. You start the Visual Data Manager from the Add-Ins menu. VisData’s four main functions are to: • Create a new database from scratch, defining its table and record structure. • Open an existing database and examine its table and record structure. • Open an existing database and add, delete, or edit records. • Create indexes for a database. Visual Data Manager is a programmer’s tool that is part of Visual Basic. It is not something you distribute with your applications. I’ll show you how to use VisData in Chapter 21.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.